package com.runfast.paotui.web.controller.user;

import com.runfast.common.dao.model.RunfastAgentbusiness;
import com.runfast.common.dao.model.RunfastShopperExample;
import com.runfast.common.service.RunfastAgentbusinessService;
import com.runfast.common.service.RunfastShopperService;
import com.runfast.common.web.controller.BaseController;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.Catalog;
import com.runfast.paotui.dao.model.PaotuiCost;
import com.runfast.paotui.dao.model.PaotuiCostWeight;
import com.runfast.paotui.dao.model.PaotuiCostWeightExample;
import com.runfast.paotui.service.PaotuiCostService;
import com.runfast.paotui.service.PaotuiCostWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 首页接口
 */
@RestController
@RequestMapping(value = "/api/user/home")
public class HomeController extends BaseController<Catalog, Integer> {

    @Autowired
    private RunfastAgentbusinessService agentbusinessService;

    @Autowired
    private RunfastShopperService shopperService;

    @Autowired
    private PaotuiCostService costService;

    @Autowired
    private PaotuiCostWeightService weightService;

    /**
     * 获取用户附件骑手数量
     *
     * @param userLng 用户经度
     * @param userLat 用户纬度
     * @return
     */
    @PostMapping("/driverCountNearBy")
    public Result driverCountNearBy(@RequestParam double userLng, @RequestParam double userLat) {
        RunfastAgentbusiness agentNearBy = agentbusinessService.getAgentNearByRange(userLng, userLat);

        if (agentNearBy == null) return Result.fail(ResultCode.AGENT_NEAR_BY_NOT_EXIST);
        RunfastShopperExample shopperExample = new RunfastShopperExample();
        shopperExample.createCriteria().andAgentIdEqualTo(agentNearBy.getId());
        long count = shopperService.countByExample(shopperExample);
        return Result.ok("", count);
    }

    /**
     * 获取跑腿基础配送费
     *
     * @param agentId
     * @return
     */
    @PostMapping("/baseDeliveryFee")
    public Result baseDeliveryFee(@RequestParam Integer agentId) {

        PaotuiCost defaultCost = costService.getDefaultCost(agentId);
        if (defaultCost != null) {
            PaotuiCostWeightExample costWeightExample = new PaotuiCostWeightExample();
            costWeightExample.or().andCostidEqualTo(defaultCost.getId());
            List<PaotuiCostWeight> costWeightList = weightService.selectByExample(costWeightExample);

            Double maxWeight = 0d;
            for (PaotuiCostWeight costWeight : costWeightList) {
                Double endweight = costWeight.getEndweight();
                if (maxWeight <= endweight) {
                    maxWeight = endweight;
                }

            }
            HashMap<String, Object> result = new HashMap<>();
            result.putAll(BeanMap.create(defaultCost));
            result.put("maxWeight", maxWeight);
            return Result.ok("", result);
        } else return Result.fail(ResultCode.FAIL, "代理商未设置跑腿配送模板");

    }


}
